from router.parseSourceUrlFromOnlyFansDetailHtml import search as onlyfansSearch
from router.parseSourceUrlFromSearchHtml import search as sihuSearch
from router.parseImgUrlFromSourceUrl import parse as parseImg
from router.parseVideoUrlFromSourceUrl import parse as parseVideo
from net_tools.downloadFiles import download
from db_tools.mysql_pool import insert
from log_tools import log_tools

logger = log_tools.get_logger()

def route(keywork, model, baseDir, save_type):
    print(f"keywork={keywork} model={model} baseDir={baseDir} save_type={save_type}")
    result = True

    sourceDict=None
    if model=="0": # onlyfans板块的下载
        sourceDict = onlyfansSearch(keywork)
        sourceDict["saveDir"] = baseDir + "OnlyFans/" + sourceDict["pageTitle"] + "/"

    elif model=="1": # 全站搜索的下载
        sourceDict = sihuSearch(keywork)
        sourceDict["saveDir"] = baseDir + "search/"
    else:
        result = False

    if result :
        print(sourceDict)
        logger.info("资源列表总数："+len(sourceDict["sourceList"]).__str__())
        if save_type == "file":
            doDownload(sourceDict)
        elif save_type == "db":
            sourceDict["keywork"]=keywork
            doSaveDB(sourceDict, keywork)


    return result

def doDownload(sourceDict):
    sourceList = sourceDict["sourceList"]
    saveDir = sourceDict["saveDir"]
    sourceNum = len(sourceList)

    for idx in range(sourceNum):

        source = sourceList[idx]
        logger.info(f"当前进度：{idx}/{sourceNum}")
        logger.info(f"当前资源：{source}")
        if "video" == source["topic"]:
            downVideo(source, saveDir)
        if "picture" == source["topic"]:
            downPic(source, saveDir)

def downVideo(source, saveDir, retry):
    voideDict = parseVideo(source["sourceUrl"])
    print(voideDict)
    if not voideDict:
        return
    fileName = voideDict["name"] +"."+ voideDict["suffix"]
    downloadRes1 = download(voideDict["url"],fileName , saveDir)
    if not downloadRes1:#重试一次
        downloadRes2 = download(voideDict["url"], fileName, saveDir, True)
        if not downloadRes2 and not retry: # 是否尝试图片
            downPic(source, saveDir, True)


def downPic(source, saveDir, retry: bool = False):
    imgDict = parseImg(source["sourceUrl"])
    print(imgDict)
    if not imgDict:
        return
    img_list = saveDir + imgDict["title"] + "/"
    imgList = imgDict["list"]
    if not imgList and not retry: # 是否尝试视频
        downVideo(source, saveDir, True)
    else:
        for img in imgList:
            downloadRes1 = download(img["url"], img["name"], img_list)
            if not downloadRes1:#重试一次
                downloadRes2 = download(img["url"], img["name"], img_list, True)





def doSaveDB(sourceDict, keywork):
    sourceList = sourceDict["sourceList"]
    sourceNum = len(sourceList)

    insert_sql = "insert into yee_source_info (sys_type,source_type,source_title,source_url,source_count,source_topic,source_params,create_time,update_time) " \
                 "values (%(sys_type)s,%(source_type)s,%(source_title)s,%(source_url)s,%(source_count)s,%(source_topic)s,%(source_params)s,NOW(),NOW())"
    insert_param = ({"sys_type":"python_sihu","source_type":"search","source_title":sourceDict["searchTitle"],"source_url":sourceDict["url"],
                     "source_count":sourceDict["searchNum"],"source_topic":"picture/video","source_params":keywork})
    info_id = insert(insert_sql,insert_param)
    for idx in range(sourceNum):

        source = sourceList[idx]
        insert_detail_sql = "insert into yee_source_detail (info_id,source_title,source_url,source_topic,source_count,create_time,update_time) " \
                            "values (%(info_id)s,%(source_title)s,%(source_url)s,%(source_topic)s,%(source_count)s,NOW(),NOW())"
        insert_detail_param = ({"info_id":info_id,"source_title":source["sourceTitle"],"source_url":source["sourceUrl"],
                                "source_topic":source["topic"],"source_count":1})
        detail_id = insert(insert_detail_sql,insert_detail_param)
        print(source)
        logger.info(f"当前进度：{idx}/{sourceNum}")
        logger.info(f"当前资源：{source}")
        if "video"==source["topic"]:
            voideDict = parseVideo(source["sourceUrl"])
            fileName = voideDict["name"] +"."+ voideDict["suffix"]
            insert_file_sql = "insert into yee_source_file (detail_id,file_url,file_name,file_suffix,file_topic,create_time,update_time) " \
                              "values (%(detail_id)s,%(file_url)s,%(file_name)s,%(file_suffix)s,%(file_topic)s,NOW(),NOW())"
            insert_file_param = ({"detail_id":detail_id,"file_url":voideDict["url"],"file_name":voideDict["name"],
                                  "file_suffix":voideDict["suffix"],"file_topic":source["topic"]})
            insert(insert_file_sql,insert_file_param)
        if "picture"==source["topic"]:
            imgDict = parseImg(source["sourceUrl"])
            imgList = imgDict["list"]

            for img in imgList:
                insert_file_sql = "insert into yee_source_file (detail_id,file_url,file_name,file_suffix,file_topic,create_time,update_time) " \
                                  "values (%(detail_id)s,%(file_url)s,%(file_name)s,%(file_suffix)s,%(file_topic)s,NOW(),NOW())"
                insert_file_param = ({"detail_id":detail_id,"file_url":img["url"],"file_name":img["name"],
                                      "file_suffix":"","file_topic":source["topic"]})
                insert(insert_file_sql,insert_file_param)